﻿//===============================================================================
// CB Stock Data Library
//===============================================================================
// Copyright © CB.  All rights reserved.
// THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, 
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS 
// OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER 
// IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 
// WITH THE CODE OR THE USE OR OTHER DEALINGS IN THE CODE.
//===============================================================================

using System;
using System.Collections.Generic;
using System.Reflection;

namespace CB.Stock.Core
{
    [AttributeUsage(AttributeTargets.Field, Inherited = false, AllowMultiple = false)]
    public sealed class StockDataValueTypeAttribute : Attribute
    {
        private readonly Type _valueType;

        // This is a positional argument
        public StockDataValueTypeAttribute(Type valueType)
        {
            _valueType = valueType;
        }

        public Type ValueType { get { return _valueType; } }

        private static readonly Dictionary<StockDataType, Type> cachedStockSataTypeDict = new Dictionary<StockDataType, Type>();
        static StockDataValueTypeAttribute()
        {
            foreach (var t in typeof(StockDataType).GetFields(BindingFlags.Static | BindingFlags.GetField | BindingFlags.Public))
            {
                var a =
                    (StockDataValueTypeAttribute)t.GetCustomAttributes(typeof(StockDataValueTypeAttribute), false)[0];
                cachedStockSataTypeDict[(StockDataType) t.GetValue(null)] = a.ValueType;
            }
        }

        public static Type GetStaockDataType(StockDataType type)
        {
            return cachedStockSataTypeDict[type];
        }
    }
}
